function [folderNSamples folderNPeaks folderNClusters folderBaseLine]=FeatureCreation(conn, folder_Index,  folderPath, runParams , isControl)

folderNSamples=0;
folderNPeaks=0;
folderNClusters=0;
folderBaseLine=0;

disp('===============================')
disp('LOADING EXPERIMENT FILES')
%do all the experiment files

files = dir([folderPath '\\*.tdms']);

if isempty(files)
    files = dir([folderPath '\\*.abf']);
end

if isempty(files)
    return
end

for k=1:length(files)
    try
        %determine if this file has already been encoded with the correct
        %parameters
        fileName =[folderPath '\' files(k).name];
        
        a=strrep(fileName,'\','//');
        
        sql =['SELECT file_Index FROM files where FileName=''' a '''' ...
            'AND Folder_Index=' num2str(folder_Index) ';'];
        
        ret=fetch(exec(conn,sql));
        
        if isstruct(ret.Data)
            alreadyDone =true;
            file_Index = ret.Data.File_Index;
        else
            alreadyDone = false;
            sql = 'SELECT max(file_Index) as m from files;';
            ret = exec(conn,sql);
            ret = fetch(ret);
            
            try
                if isnan(ret.Data.m)
                    file_Index=1;
                else
                    if isempty(ret.Message)==false  || isstruct(ret.Data)==false
                        file_Index=1;
                    else
                        file_Index=ret.Data.m+1;
                    end
                end
            catch
                file_Index=1;
            end
        end
        
        if (alreadyDone == false)
            %if it has not, then do the collection
            disp(['Loading: ']); %='21May2012_001.tdms';
            disp (k);
            disp('*******************************')
            disp('LoadAndFilter')
            %load the trace from this file, remove the background and the
            %high frequencies
            [trace ] =LoadAndFilter(folderPath,files(k).name,runParams);
            
            [allStarts, allEnds] = PeakRangeFinder( trace,  runParams );
            
            if isempty(allStarts)==false
                [numPeaks, numClusters]= ClusterPeakParameters(conn,folder_Index,file_Index,trace, allStarts, allEnds,runParams );
                  [ Hz60, baseLine  ] = GetTraceQuality( trace );
            else
              numPeaks=0;
              numClusters=0;
              Hz60=1000;
              baseLine=1000;
            end
            
          
            
            sql =['INSERT INTO files (Folder_Index,FileName,numSamples,numPeaks,numClusters,BaselineVariance,_60Hz) VALUE (' ...
                num2str( folder_Index)  ',''' a ''',' num2str(length(trace)) ',' num2str(numPeaks) ',' num2str(numClusters) ',' num2str(baseLine) ',' num2str(Hz60) ');' ]
            exec(conn,sql);
            
            
            folderNSamples=folderNSamples + length(trace);
            folderNPeaks=folderNPeaks +numPeaks ;
            folderNClusters=folderNClusters + numClusters;
            folderBaseLine=folderBaseLine + baseLine;
        end
    catch mex
        disp(mex)
        disp(mex.stack)
        disp(mex.stack(1,1));
        
    end
end
% sql=['INSERT or REPLACE INTO runParams (folderPath, folder_index, nPeaks, nClusters, nSamples) VALUES ('];
folderBaseLine=folderBaseLine/k;
end